package com.Jano.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.util.Objects;

public class MyBatisUtils {
    private static SqlSessionFactory factory;

    static {
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        try {
            factory = builder.build(Resources.getResourceAsReader("SqlMapConfig.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private MyBatisUtils() {

    }

    public static SqlSession openSession() {
        return factory.openSession();
    }

    public static void closeSession(SqlSession session) {
        if (Objects.nonNull(session)) {
            session.close();
        }
    }

}
